package com.cwj.mansysback.servlet.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cwj.mansysback.entity.Student;
import com.cwj.mansysback.entity.Teacher;
import com.cwj.mansysback.service.api.AccountService;
import com.cwj.mansysback.service.api.StudentService;
import com.cwj.mansysback.service.api.TeacherService;
import com.cwj.mansysback.service.impl.AccountServiceImpl;
import com.cwj.mansysback.service.impl.StudentServiceImpl;
import com.cwj.mansysback.service.impl.TeacherServiceImpl;
import com.cwj.mansysback.servlet.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddAccServlet extends HttpServlet {

    private static final String accKeys[] = {"Admin", "Teacher", "Student"};

    TeacherService teaSer = new TeacherServiceImpl();

    AccountService accSer = new AccountServiceImpl();

    StudentService stuSer = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        super.doGet(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("----------------------------- AddAccServlet start ----------------------------");

        boolean flag = false;
        Statement sta = null;


        if (req.getRequestURI().contains(AddAccServlet.accKeys[1])) {

            String json = req.getParameter("teacher");

            System.out.println(json);

            if (json != null) {

                Teacher jsonObject = JSON.parseObject(json, Teacher.class);

                jsonObject.getRid();
                jsonObject.setAccrank(Teacher.getRANK());

                if (teaSer.getTeacher(jsonObject.getAid()) != null) {
                    MyUtils.writeResponse(resp, new Statement(13));
                    return;
                }

                System.out.println(jsonObject);

                flag = teaSer.addTeaTeacher(jsonObject);

            }


        }else if(req.getRequestURI().contains("/Lock/Add/Acc/Stud")){
            String stud = req.getParameter("student");
            System.out.println(stud);
            if(stud != null){
                Student student = JSON.parseObject(stud,Student.class);
                student.setAccrank(Student.getRANK());
                System.out.println(student);
                if(stuSer.selectStudent(student.getAid()) != null){
                    MyUtils.writeResponse(resp, new Statement(13));
                    return;
                }

                flag = stuSer.addStudent(student);
            }
        }
        else {
            flag = false;
        }

        if (flag) {
            sta = new Statement(0);
        } else {
            sta = new Statement(12);
        }

        MyUtils.writeResponse(resp, sta);

        System.out.println("----------------------------- AddAccServlet end ----------------------------");

    }
}
